<template>
    <div class="nginx-feature-config">
        <!-- 配置卡片网格 -->
        <div class="config-grid">
            <div 
                v-for="feature in allFeatures" 
                :key="feature.id"
                class="config-card"
                :class="{ active: selectedFeature?.id === feature.id }"
                @click="selectFeature(feature)"
            >
                <div class="card-icon">
                    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                        <path d="M14 2H6C5.46957 2 4.96086 2.21071 4.58579 2.58579C4.21071 2.96086 4 3.46957 4 4V20C4 20.5304 4.21071 21.0391 4.58579 21.4142C4.96086 21.7893 5.46957 22 6 22H18C18.5304 22 19.0391 21.7893 19.4142 21.4142C19.7893 21.0391 20 20.5304 20 20V8L14 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    </svg>
                </div>
                
                <div class="card-content">
                    <h3 class="card-title">{{ feature.name }}</h3>
                    <div class="card-category">{{ feature.category }}</div>
                </div>
            </div>
        </div>

        <!-- 配置详情 -->
        <div v-if="selectedFeature" class="feature-detail">
            <div class="detail-header">
                <div class="detail-title">
                    <h2>{{ selectedFeature.name }}</h2>
                    <p>{{ selectedFeature.description }}</p>
                </div>
                <div class="detail-actions">
                    <c-button @click="copyTemplate" :loading="isCopying" type="primary" size="small">
                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path d="M16 4H18C18.5304 4 19.0391 4.21071 19.4142 4.58579C19.7893 4.96086 20 5.46957 20 6V18C20 18.5304 19.7893 19.0391 19.4142 19.4142C19.0391 19.7893 18.5304 20 18 20H16" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                            <path d="M8 20H6C5.46957 20 4.96086 19.7893 4.58579 19.4142C4.21071 19.0391 4 18.5304 4 18V6C4 5.46957 4.21071 4.96086 4.58579 4.58579C4.96086 4.21071 5.46957 4 6 4H8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                        </svg>
                        复制配置
                    </c-button>
                </div>
            </div>

            <!-- 配置模板 -->
            <div class="config-section">
                <div class="section-header">
                    <h3>配置模板</h3>
                    <div class="highlight-legend">
                        <div class="legend-item">
                            <span class="legend-color important"></span>
                            <span>核心配置</span>
                        </div>
                        <div class="legend-item">
                            <span class="legend-color warning"></span>
                            <span>基本配置</span>
                        </div>
                        <div class="legend-item">
                            <span class="legend-color info"></span>
                            <span>辅助配置</span>
                        </div>
                        <div class="legend-item">
                            <span class="legend-color success"></span>
                            <span>辅助配置</span>
                        </div>
                    </div>
                </div>
                
                <div class="config-code">
                    <div 
                        v-for="(line, index) in configLines" 
                        :key="index"
                        class="code-line"
                        :class="getLineHighlightClass(index + 1)"
                    >
                        <span class="line-number">{{ index + 1 }}</span>
                        <span class="line-content">{{ line }}</span>
                        <div v-if="getLineHighlight(index + 1)" class="line-highlight">
                            {{ getLineHighlight(index + 1)?.description }}
                        </div>
                    </div>
                </div>
            </div>

            <!-- 文档说明 -->
            <div v-if="selectedFeature.documentation" class="documentation-section">
                <h3>使用说明</h3>
                <div class="documentation-content">
                    {{ selectedFeature.documentation }}
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { useCopy } from '@/composable/copy';
import { nginxFeatures, type NginxFeature } from './features';

const selectedFeature = ref<NginxFeature | null>(null);
const isCopying = ref(false);

const { copy } = useCopy();

// 所有功能列表
const allFeatures = computed(() => nginxFeatures);

// 配置代码行
const configLines = computed(() => {
    if (!selectedFeature.value) return [];
    return selectedFeature.value.template.split('\n');
});

// 选择功能
const selectFeature = (feature: NginxFeature) => {
    selectedFeature.value = feature;
};

// 复制配置模板
const copyTemplate = async () => {
    if (!selectedFeature.value) return;
    
    isCopying.value = true;
    try {
        await copy(selectedFeature.value.template);
    } finally {
        isCopying.value = false;
    }
};

// 获取行高亮类型
const getLineHighlightClass = (lineNumber: number) => {
    const highlight = getLineHighlight(lineNumber);
    if (!highlight) return '';
    return `highlight-${highlight.type}`;
};

// 获取行高亮信息
const getLineHighlight = (lineNumber: number) => {
    if (!selectedFeature.value) return null;
    return selectedFeature.value.highlights.find(h => 
        lineNumber >= h.start && lineNumber <= h.end
    );
};
</script>

<style scoped>
.nginx-feature-config {
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem 1rem;
}

/* 配置卡片网格 */
.config-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    margin: 2rem 0 2rem 0;
}

.config-card {
    background: white;
    border: 2px solid #e2e8f0;
    border-radius: 12px;
    padding: 1.25rem;
    cursor: pointer;
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    display: flex;
    align-items: center;
    gap: 1rem;
}

.config-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    transform: scaleX(0);
    transition: transform 0.3s ease;
}

.config-card:hover {
    border-color: #667eea;
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(102, 126, 234, 0.15);
}

.config-card:hover::before {
    transform: scaleX(1);
}

.config-card.active {
    border-color: #667eea;
    background: linear-gradient(135deg, #f8faff 0%, #f1f5ff 100%);
    box-shadow: 0 8px 25px rgba(102, 126, 234, 0.2);
}

.config-card.active::before {
    transform: scaleX(1);
}

.card-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 8px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    flex-shrink: 0;
}

.card-content {
    flex: 1;
    min-width: 0;
}

.card-title {
    font-size: 1rem;
    font-weight: 600;
    margin: 0 0 0.5rem 0;
    color: #1e293b;
    line-height: 1.3;
}

.card-category {
    background: rgba(102, 126, 234, 0.1);
    color: #667eea;
    padding: 0.2rem 0.6rem;
    border-radius: 12px;
    font-size: 0.7rem;
    font-weight: 600;
    display: inline-block;
    border: 1px solid rgba(102, 126, 234, 0.2);
}

/* 配置详情 */
.feature-detail {
    background: white;
    border-radius: 16px;
    padding: 2rem;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    border: 1px solid #e2e8f0;
}

.detail-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 2rem;
    padding-bottom: 1.5rem;
    border-bottom: 1px solid #e2e8f0;
}

.detail-title h2 {
    font-size: 1.75rem;
    font-weight: 700;
    margin: 0 0 0.75rem 0;
    color: #1e293b;
}

.detail-title p {
    color: #64748b;
    margin: 0;
    line-height: 1.6;
    font-size: 1rem;
}

/* 配置代码 */
.config-section {
    margin-bottom: 2rem;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
}

.section-header h3 {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0;
    color: #1e293b;
}

.highlight-legend {
    display: flex;
    gap: 1rem;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    font-size: 0.75rem;
    color: #64748b;
}

.legend-color {
    width: 12px;
    height: 12px;
    border-radius: 2px;
}

.legend-color.important { background: #ef4444; }
.legend-color.warning { background: #f59e0b; }
.legend-color.info { background: #3b82f6; }
.legend-color.success { background: #10b981; }

.config-code {
    background: #1e293b;
    border-radius: 12px;
    padding: 1.5rem;
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: 0.875rem;
    line-height: 1.6;
    overflow-x: auto;
    border: 1px solid #374151;
}

.code-line {
    display: flex;
    align-items: flex-start;
    gap: 1rem;
    padding: 0.25rem 0;
    position: relative;
}

.code-line:hover {
    background: rgba(255, 255, 255, 0.05);
}

.line-number {
    color: #64748b;
    min-width: 2.5rem;
    text-align: right;
    user-select: none;
    font-size: 0.8rem;
}

.line-content {
    color: #e2e8f0;
    flex: 1;
    white-space: pre;
}

.line-highlight {
    position: absolute;
    right: 0;
    top: 100%;
    background: #374151;
    color: #e2e8f0;
    padding: 0.75rem;
    border-radius: 8px;
    font-size: 0.8rem;
    max-width: 350px;
    z-index: 10;
    opacity: 0;
    transition: opacity 0.2s ease;
    pointer-events: none;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.code-line:hover .line-highlight {
    opacity: 1;
}

/* 高亮样式 */
.code-line.highlight-important {
    background: rgba(239, 68, 68, 0.1);
    border-left: 4px solid #ef4444;
    margin-left: -1.5rem;
    padding-left: 1.5rem;
}

.code-line.highlight-warning {
    background: rgba(245, 158, 11, 0.1);
    border-left: 4px solid #f59e0b;
    margin-left: -1.5rem;
    padding-left: 1.5rem;
}

.code-line.highlight-info {
    background: rgba(59, 130, 246, 0.1);
    border-left: 4px solid #3b82f6;
    margin-left: -1.5rem;
    padding-left: 1.5rem;
}

.code-line.highlight-success {
    background: rgba(16, 185, 129, 0.1);
    border-left: 4px solid #10b981;
    margin-left: -1.5rem;
    padding-left: 1.5rem;
}

/* 文档说明 */
.documentation-section h3 {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0 0 1rem 0;
    color: #1e293b;
}

.documentation-content {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    padding: 1.5rem;
    color: #64748b;
    line-height: 1.7;
    font-size: 0.95rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .nginx-feature-config {
        padding: 1rem 0.5rem;
    }
    
    .config-grid {
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
        gap: 0.75rem;
    }
    
    .config-card {
        padding: 1rem;
        gap: 0.75rem;
    }
    
    .detail-header {
        flex-direction: column;
        gap: 1rem;
    }
    
    .section-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.75rem;
    }
    
    .highlight-legend {
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    
    .config-code {
        padding: 1rem;
        font-size: 0.8rem;
    }
    
    .feature-detail {
        padding: 1.5rem;
    }
}

@media (max-width: 480px) {
    .card-title {
        font-size: 0.9rem;
    }
    
    .card-icon {
        width: 28px;
        height: 28px;
    }
    
    .card-category {
        font-size: 0.65rem;
        padding: 0.15rem 0.5rem;
    }
    
    .detail-title h2 {
        font-size: 1.5rem;
    }
}
</style> 